package com.andy.qi.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.andy.qi.domain.Game;
import com.andy.qi.domain.GameArchive;
import com.andy.qi.domain.GamePK;
import com.andy.qi.domain.User;
import com.andy.qi.domain.nogame.NoHistory;
import com.andy.qi.domain.room.Room;
import com.andy.qi.repository.GameArchiveRepository;
import com.andy.qi.repository.GameRepository;
import com.andy.qi.repository.RoomRepository;
import com.andy.qi.repository.UserRepository;
import com.andy.qi.util.Json;
import com.andy.qi.util.TimeUtils;
import com.fasterxml.jackson.core.type.TypeReference;

@Component
public class DBUtils {
    @Autowired private GameRepository gameRepository;
    @Autowired private GameArchiveRepository gameArchiveRepository;
    @Autowired private RoomRepository roomRepository;
    @Autowired private UserRepository userRepository;

    public void update_game_action_records(String roomUuid, int gameIndex, String action_records) {
        Game game = gameRepository.findOneByIdRoomUuidAndIdGameIndex(roomUuid, gameIndex);
        game.setAction_records(action_records);
        gameRepository.save(game);
    }

    public void update_num_of_turns(String roomId, int numOfGames) {
        Room room = roomRepository.findOneById(roomId);
        room.numOfGames = numOfGames;
        roomRepository.save(room);
    }

    public void update_game_result(String roomUuid, int gameIndex, int[] dbresult) {
        Game game = gameRepository.findOneByIdRoomUuidAndIdGameIndex(roomUuid, gameIndex);
        game.setResult(Json.toJson(dbresult));
        gameRepository.save(game);
    }

    public void update_next_button(String roomId, int nextButton) {
        Room room = roomRepository.findOneById(roomId);
        room.nextButton = nextButton;
        roomRepository.save(room);
    }

    public void cost_gems(String userId, int cost) {
        User user = userRepository.findOne(userId);
        user.setGems(user.getGems() - cost);
        userRepository.save(user);
    }

    public List<NoHistory> get_user_history(String userId) {
        User user = userRepository.findOne(userId);
        return Json.fromJson(user.getHistory(), new TypeReference<ArrayList<NoHistory>>(){});
    }

    public void update_user_history(String userId, List<NoHistory> data) {
        User user = userRepository.findOne(userId);
        user.setHistory(Json.toJson(data));
        userRepository.save(user);
    }

    public void create_game(String uuid, int gameIndex, String base_info) {
        Game game = new Game();
        game.setId(new GamePK(uuid, gameIndex));
        game.setBase_info(base_info);
        game.setCreate_time(TimeUtils.now());
        gameRepository.save(game);
    }

    public void set_room_id_of_user(String userId, String roomid) {
        User user = userRepository.findOne(userId);
        user.setRoomid(roomid);
        userRepository.save(user);
    }

    public void archive_games(String uuid) {
        List<Game> games = gameRepository.findAllByIdRoomUuid(uuid);
        if(games != null  && !games.isEmpty()) {
            for(Game game : games) {
                GameArchive ga = new GameArchive();
                BeanUtils.copyProperties(game, ga);
                gameArchiveRepository.save(ga);
            }
        }
    }
}
